package com.github.shiqiyue.app.api.gapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.shiqiyue.app.api.gapi.common.GApiBaseController;
import com.github.shiqiyue.app.api.gapi.req.notice.NoticePageReq;
import com.github.shiqiyue.app.api.gapi.result.notice.NoticePageResult;
import com.github.shiqiyue.app.common.result.Result;
import com.github.shiqiyue.app.modules.app.entity.Notice;
import com.github.shiqiyue.app.modules.app.enums.NoticeStatusEnum;
import com.github.shiqiyue.app.modules.app.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

/***
 *
 * @author wenyao.wu
 */
@Api(tags = "公告")
@RestController
@RequestMapping("gapi/notice")
public class GApiNoticeController  extends GApiBaseController {

    @Autowired
    private INoticeService noticeService;

    @ApiOperation("公告列表")
    @PostMapping("page")
    public Result<NoticePageResult> noticePage(@RequestBody  NoticePageReq noticePageReq){
        QueryWrapper<Notice> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq(Notice.Dict.STATUS, NoticeStatusEnum.ON.getValue());
        queryWrapper.orderByDesc(Notice.Dict.ID);
        IPage<Notice> noticePage =noticeService.page(new Page<>(noticePageReq.getCurrent(), noticePageReq.getSize()), queryWrapper);
        NoticePageResult noticePageResult = new NoticePageResult();
        noticePageResult.setPageInfo(noticePage);
        if(CollectionUtils.isNotEmpty(noticePage.getRecords())){
            noticePageResult.setRecords(noticePage.getRecords().stream().map((item)->{
                NoticePageResult.Item r = new NoticePageResult.Item();
                r.setId(item.getId());
                r.setTitle(item.getTitle());
                r.setContent(item.getContent());
                return r;
            }).collect(Collectors.toList()));
        }
        return Result.success(noticePageResult);
    }
}
